home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / code / list.lisp < prev    next >
Encoding:
Text File  |  1992-05-30  |  33.5 KB  |  977 lines

  1. ;;; -*- Log: code.log; Package: Lisp -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the CMU Common Lisp project at
  5. ;;; Carnegie Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of CMU Common Lisp, please contact
  7. ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
  8. ;;;
  9. (ext:file-comment
  10.   "$Header: list.lisp,v 1.10 92/01/27 10:56:05 ram Exp $")
  11. ;;;
  12. ;;; **********************************************************************
  13. ;;;
  14. ;;; Functions to implement lists for Spice Lisp.
  15. ;;; Written by Joe Ginder and Carl Ebeling.
  16. ;;; Rewritten and currently maintained by Skef Wholey.
  17. ;;; 
  18. ;;; Nsublis, things at the beginning broken.
  19. ;;;
  20. ;;; The list functions are part of the standard Spice Lisp environment.
  21. ;;;
  22. ;;; **********************************************************************
  23. ;;;
  24. (in-package 'lisp)
  25.  
  26. (export '(car cdr caar
  27.       cadr cdar cddr caaar caadr cadar caddr cdaar cdadr
  28.       cddar cdddr caaaar caaadr caadar caaddr cadaar cadadr
  29.       caddar cadddr cdaaar cdaadr cdadar cdaddr cddaar cddadr
  30.       cdddar cddddr cons tree-equal endp list-length nth first
  31.       second third fourth fifth sixth seventh eighth
  32.       ninth tenth rest nthcdr last list list* make-list
  33.       append copy-list copy-alist copy-tree revappend nconc
  34.       nreconc butlast nbutlast ldiff rplaca rplacd subst
  35.       subst-if subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis
  36.       member member-if member-if-not tailp adjoin union
  37.       nunion intersection nintersection set-difference
  38.       nset-difference set-exclusive-or nset-exclusive-or subsetp
  39.       acons pairlis
  40.       assoc assoc-if assoc-if-not
  41.       rassoc rassoc-if rassoc-if-not
  42.       complement constantly))
  43.  
  44. (proclaim '(maybe-inline
  45.         tree-equal list-length nth %setnth nthcdr last make-list append
  46.         copy-list copy-alist copy-tree revappend nconc nreconc butlast
  47.         nbutlast ldiff member member-if member-if-not tailp adjoin union
  48.         nunion intersection nintersection set-difference nset-difference
  49.         set-exclusive-or nset-exclusive-or subsetp acons pairlis assoc
  50.         assoc-if assoc-if-not rassoc rassoc-if rassoc-if-not subst subst-if
  51.         subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
  52.  
  53.  
  54. (in-package "EXTENSIONS")
  55. (export '(assq memq delq))
  56. (proclaim '(inline assq memq))
  57. (proclaim '(maybe-inline delq))
  58. (in-package 'lisp)
  59.  
  60.  
  61. ;;; These functions perform basic list operations:
  62.  
  63. (defun car (list) "Returns the 1st object in a list." (car list))
  64. (defun cdr (list) "Returns all but the first object." (cdr list))
  65. (defun cadr (list) "Returns the 2nd object in a list." (cadr list))
  66. (defun cdar (list) "Returns the cdr of the 1st sublist." (cdar list))
  67. (defun caar (list) "Returns the car of the 1st sublist." (caar list))
  68. (defun cddr (list) "Returns all but the 1st two objects of a list." (cddr list))
  69. (defun caddr (list) "Returns the 1st object in the cddr of a list." (caddr list))
  70. (defun caadr (list) "Returns the 1st object in the cadr of a list." (caadr list))
  71. (defun caaar (list) "Returns the 1st object in the caar of a list." (caaar list))
  72. (defun cdaar (list) "Returns the cdr of the caar of a list." (cdaar list))
  73. (defun cddar (list) "Returns the cdr of the cdar of a list." (cddar list))
  74. (defun cdddr (list) "Returns the cdr of the cddr of a list." (cdddr list))
  75. (defun cadar (list) "Returns the car of the cdar of a list." (cadar list))
  76. (defun cdadr (list) "Returns the cdr of the cadr of a list." (cdadr list))
  77. (defun caaaar (list) "Returns the car of the caaar of a list." (caaaar list))
  78. (defun caaadr (list) "Returns the car of the caadr of a list." (caaadr list))
  79. (defun caaddr (list) "Returns the car of the caddr of a list." (caaddr list))
  80. (defun cadddr (list) "Returns the car of the cdddr of a list." (cadddr list))
  81. (defun cddddr (list) "Returns the cdr of the cdddr of a list." (cddddr list))
  82. (defun cdaaar (list) "Returns the cdr of the caaar of a list." (cdaaar list))
  83. (defun cddaar (list) "Returns the cdr of the cdaar of a list." (cddaar list))
  84. (defun cdddar (list) "Returns the cdr of the cddar of a list." (cdddar list))
  85. (defun caadar (list) "Returns the car of the cadar of a list." (caadar list))
  86. (defun cadaar (list) "Returns the car of the cdaar of a list." (cadaar list))
  87. (defun cadadr (list) "Returns the car of the cdadr of a list." (cadadr list))
  88. (defun caddar (list) "Returns the car of the cddar of a list." (caddar list))
  89. (defun cdaadr (list) "Returns the cdr of the caadr of a list." (cdaadr list))
  90. (defun cdadar (list) "Returns the cdr of the cadar of a list." (cdadar list))
  91. (defun cdaddr (list) "Returns the cdr of the caddr of a list." (cdaddr list))
  92. (defun cddadr (list) "Returns the cdr of the cdadr of a list." (cddadr list))
  93. (defun cons (se1 se2) "Returns a list with se1 as the car and se2 as the cdr."
  94.                       (cons se1 se2))
  95.  
  96.  
  97. (proclaim '(maybe-inline tree-equal-test tree-equal-test-not))
  98.  
  99. (defun tree-equal-test-not (x y test-not)
  100.   (cond ((not (funcall test-not x y)) t)
  101.     ((consp x)
  102.      (and (consp y)
  103.           (tree-equal-test-not (car x) (car y) test-not)
  104.           (tree-equal-test-not (cdr x) (cdr y) test-not)))
  105.     (t ())))
  106.  
  107. (defun tree-equal-test (x y test)
  108.   (cond ((funcall test x y) t)
  109.     ((consp x)
  110.      (and (consp y)
  111.           (tree-equal-test (car x) (car y) test)
  112.           (tree-equal-test (cdr x) (cdr y) test)))
  113.     (t ())))
  114.  
  115. (defun tree-equal (x y &key (test #'eql) test-not)
  116.   "Returns T if X and Y are isomorphic trees with identical leaves."
  117.   (if test-not
  118.       (tree-equal-test-not x y test-not)
  119.       (tree-equal-test x y test)))
  120.  
  121.  
  122. (defun endp (object)
  123.   "The recommended way to test for the end of a list.  True if Object is nil,
  124.    false if Object is a cons, and an error for any other types of arguments."
  125.   (endp object))
  126.  
  127. (defun list-length (list)
  128.   "Returns the length of the given List, or Nil if the List is circular."
  129.   (do ((n 0 (+ n 2))
  130.        (y list (cddr y))
  131.        (z list (cdr z)))
  132.       (())
  133.     (declare (fixnum n) (list y z))
  134.     (when (endp y) (return n))
  135.     (when (endp (cdr y)) (return (+ n 1)))
  136.     (when (and (eq y z) (> n 0)) (return nil))))
  137.  
  138. (defun nth (n list)
  139.   "Returns the nth object in a list where the car is the zero-th element."
  140.   (car (nthcdr n list)))
  141.  
  142. (defun first (list)
  143.   "Returns the 1st object in a list or NIL if the list is empty."
  144.   (car list))
  145. (defun second (list)
  146.   "Returns the 2nd object in a list or NIL if there is no 2nd object."
  147.   (cadr list))
  148. (defun third (list)
  149.   "Returns the 3rd object in a list or NIL if there is no 3rd object."
  150.   (caddr list))
  151. (defun fourth (list)
  152.   "Returns the 4th object in a list or NIL if there is no 4th object."
  153.   (cadddr list))
  154. (defun fifth (list)
  155.   "Returns the 5th object in a list or NIL if there is no 5th object."
  156.   (car (cddddr list)))
  157. (defun sixth (list)
  158.   "Returns the 6th object in a list or NIL if there is no 6th object."
  159.   (cadr (cddddr list)))
  160. (defun seventh (list)
  161.   "Returns the 7th object in a list or NIL if there is no 7th object."
  162.   (caddr (cddddr list)))
  163. (defun eighth (list)
  164.   "Returns the 8th object in a list or NIL if there is no 8th object."
  165.   (cadddr (cddddr list)))
  166. (defun ninth (list)
  167.   "Returns the 9th object in a list or NIL if there is no 9th object."
  168.   (car (cddddr (cddddr list))))
  169. (defun tenth (list)
  170.   "Returns the 10th object in a list or NIL if there is no 10th object."
  171.   (cadr (cddddr (cddddr list))))
  172. (defun rest (list)
  173.   "Means the same as the cdr of a list."
  174.   (cdr list))
  175.  
  176. (defun nthcdr (n list)
  177.   (declare (type index n))
  178.   "Performs the cdr function n times on a list."
  179.   (do ((i n (1- i))
  180.        (result list (cdr result)))
  181.       ((not (plusp i)) result)
  182.       (declare (type index i))))
  183.  
  184. (defun last (list &optional (n 1))
  185.   "Returns the last N conses (not the last element!) of a list."
  186.   (declare (type index n))
  187.   (do ((checked-list list (cdr checked-list))
  188.        (returned-list list)
  189.        (index 0 (1+ index)))
  190.       ((atom checked-list) returned-list)
  191.     (declare (type index index))
  192.     (if (>= index n)
  193.     (pop returned-list))))
  194.  
  195. (defun list (&rest args)
  196.   "Returns constructs and returns a list of its arguments."
  197.   args)
  198.  
  199. ;;; List* is done the same as list, except that the last cons is made a
  200. ;;; dotted pair
  201.  
  202. (defun list* (arg &rest others)
  203.   "Returns a list of the arguments with last cons a dotted pair"
  204.   (cond ((atom others) arg)
  205.     ((atom (cdr others)) (cons arg (car others)))
  206.     (t (do ((x others (cdr x)))
  207.            ((null (cddr x)) (rplacd x (cadr x))))
  208.        (cons arg others))))
  209.  
  210. (defun make-list (size &key initial-element)
  211.   (declare (fixnum size))
  212.   "Constructs a list with size elements each set to value"
  213.   (if (< size 0) (error "~S is an illegal size for MAKE-LIST." size)
  214.       (do ((count size (1- count))
  215.        (result '() (cons initial-element result)))
  216.       ((zerop count) result)
  217.     (declare (fixnum count)))))
  218.  
  219. ;;; The outer loop finds the first non-null list and the result is started.
  220. ;;; The remaining lists in the arguments are tacked to the end of the result
  221. ;;; using splice which cdr's down the end of the new list
  222.  
  223. (defun append (&rest lists)
  224.   "Construct a new list by concatenating the list arguments"
  225.   (do ((top lists (cdr top)))     ;;Cdr to first non-null list.
  226.       ((atom top) '())
  227.     (cond ((null (car top)))                ; Nil -> Keep looping
  228.       ((not (consp (car top)))            ; Non cons
  229.        (if (cdr top)
  230.            (error "~S is not a list." (car top))
  231.            (return (car top))))
  232.       (t                        ; Start appending
  233.        (return
  234.          (if (atom (cdr top))
  235.          (car top)    ;;Special case.
  236.          (let* ((result (cons (caar top) '())) 
  237.             (splice result))
  238.            (do ((x (cdar top) (cdr x)))  ;;Copy first list
  239.                ((atom x))
  240.              (setq splice
  241.                (cdr (rplacd splice (cons (car x) ()) ))) )
  242.            (do ((y (cdr top) (cdr y)))     ;;Copy rest of lists.
  243.                ((atom (cdr y))
  244.             (setq splice (rplacd splice (car y)))
  245.             result)
  246.              (if (listp (car y))
  247.              (do ((x (car y) (cdr x)))   ;;Inner copy loop.
  248.                  ((atom x))
  249.                (setq
  250.                 splice
  251.                 (cdr (rplacd splice (cons (car x) ())))))
  252.              (error "~S is not a list." (car y)))))))))))
  253.   
  254.  
  255. ;;; List Copying Functions
  256.  
  257.  
  258. ;;; The list is copied correctly even if the list is not terminated by ()
  259. ;;; The new list is built by cdr'ing splice which is always at the tail
  260. ;;; of the new list
  261.  
  262. (defun copy-list (list)
  263.   "Returns a new list EQUAL but not EQ to list"
  264.   (if (atom list)
  265.       (if list
  266.       (error "~S is not a list." list))
  267.       (let ((result (cons (car list) '()) ))
  268.     (do ((x (cdr list) (cdr x))
  269.          (splice result
  270.              (cdr (rplacd splice (cons (car x) '() ))) ))
  271.         ((atom x) (unless (null x)
  272.                   (rplacd splice x))
  273.               result)))))
  274.  
  275. (defun copy-alist (alist)
  276.   "Returns a new association list equal to alist, constructed in space"
  277.   (if (atom alist)
  278.       (if alist
  279.       (error "~S is not a list." alist))
  280.       (let ((result
  281.          (cons (if (atom (car alist))
  282.                (car alist)
  283.                (cons (caar alist) (cdar alist)) )
  284.            '() )))          
  285.     (do ((x (cdr alist) (cdr x))
  286.          (splice result
  287.              (cdr (rplacd splice
  288.                   (cons
  289.                    (if (atom (car x)) 
  290.                        (car x)
  291.                        (cons (caar x) (cdar x)))
  292.                    '() ))) ))
  293. ;;; Non-null terminated alist done here.
  294.         ((atom x) (unless (null x)
  295.                   (rplacd splice x))
  296.               result)))))
  297.  
  298. (defun copy-tree (object)
  299.   "Copy-Tree recursively copys trees of conses."
  300.   (cond ((not (consp object)) object)
  301.     (T (cons (copy-tree (car object)) (copy-tree (cdr object)))) ))
  302.  
  303. ;;; More Commonly-used List Functions
  304.  
  305. (defun revappend (x y)
  306.   "Returns (append (reverse x) y)"
  307.   (do ((top x (cdr top))
  308.        (result y (cons (car top) result)))
  309.       ((endp top) result)))
  310.  
  311. ;;; NCONC finds the first non-null list, so it can make splice point to a cons.
  312. ;;; After finding the first cons element, it holds it in a result variable
  313. ;;; while running down successive elements tacking them together.  While
  314. ;;; tacking lists together, if we encounter a null list, we set the previous
  315. ;;; list's last cdr to nil just in case it wasn't already nil, and it could
  316. ;;; have been dotted while the null list was the last argument to NCONC.  The
  317. ;;; manipulation of splice (that is starting it out on a first cons, setting
  318. ;;; LAST of splice, and setting splice to ele) inherently handles (nconc x x),
  319. ;;; and it avoids running down the last argument to NCONC which allows the last
  320. ;;; argument to be circular.
  321. ;;;
  322. (defun nconc (&rest lists)
  323.   "Concatenates the lists given as arguments (by changing them)"
  324.   (do ((top lists (cdr top)))
  325.       ((null top) nil)
  326.     (let ((top-of-top (car top)))
  327.       (typecase top-of-top
  328.     (cons
  329.      (let* ((result top-of-top)
  330.         (splice result))
  331.        (do ((elements (cdr top) (cdr elements)))
  332.            ((endp elements))
  333.          (let ((ele (car elements)))
  334.            (typecase ele
  335.          (cons (rplacd (last splice) ele)
  336.                (setf splice ele))
  337.          (null (rplacd (last splice) nil))
  338.          (atom (if (cdr elements)
  339.                (error "Argument is not a list -- ~S." ele)
  340.                (rplacd (last splice) ele)))
  341.          (t (error "Argument is not a list -- ~S." ele)))))
  342.        (return result)))
  343.     (null)
  344.     (atom
  345.      (if (cdr top)
  346.          (error "Argument is not a list -- ~S." top-of-top)
  347.          (return top-of-top)))
  348.     (t (error "Argument is not a list -- ~S." top-of-top))))))
  349.  
  350. (defun nreconc (x y)
  351.   "Returns (nconc (nreverse x) y)"
  352.   (do ((1st (cdr x) (if (atom 1st) 1st (cdr 1st)))
  353.        (2nd x 1st)        ;2nd follows first down the list.
  354.        (3rd y 2nd))        ;3rd follows 2nd down the list.
  355.       ((atom 2nd) 3rd)
  356.     (rplacd 2nd 3rd)))
  357.  
  358. (defun butlast (list &optional (n 1))
  359.   "Returns a new list the same as List without the N last elements."
  360.   (declare (list list) (type index n))
  361.   (let ((length (1- (length list))))
  362.     (declare (type index length))
  363.     (if (< length n)
  364.         ()
  365.         (do* ((top (cdr list) (cdr top))
  366.               (result (list (car list)))
  367.               (splice result)
  368.               (count length (1- count)))
  369.              ((= count n) result)
  370.           (declare (type index count))
  371.           (setq splice (cdr (rplacd splice (list (car top)))))))))
  372.  
  373. (defun nbutlast (list &optional (n 1))
  374.   "Modifies List to remove the last N elements."
  375.   (declare (list list) (type index n))
  376.   (let ((length (1- (length list))))
  377.     (declare (type index length))
  378.     (if (< length n) ()
  379.         (do ((1st (cdr list) (cdr 1st))
  380.              (2nd list 1st)
  381.              (count length (1- count)))
  382.             ((= count n)
  383.              (rplacd 2nd ())
  384.              list)
  385.           (declare (type index count))))))
  386.  
  387. (defun ldiff (list sublist)
  388.   "Returns a new list, whose elements are those of List that appear before
  389.    Sublist.  If Sublist is not a tail of List, a copy of List is returned."
  390.   (do* ((list list (cdr list))
  391.     (result (list ()))
  392.     (splice result))
  393.        ((or (null list) (eq list sublist)) (cdr result))
  394.     (setq splice (cdr (rplacd splice (list (car list)))))))
  395.  
  396. ;;; Functions to alter list structure
  397.  
  398. (defun rplaca (x y)
  399.   "Changes the car of x to y and returns the new x."
  400.   (rplaca x y))
  401.  
  402. (defun rplacd (x y)
  403.   "Changes the cdr of x to y and returns the new x."
  404.   (rplacd x y))
  405.  
  406. ;;; The following are for use by SETF.
  407.  
  408. (defun %rplaca (x val) (rplaca x val) val)
  409.  
  410. (defun %rplacd (x val) (rplacd x val) val)
  411.  
  412. (defun %setnth (n list newval)
  413.   (declare (fixnum n))
  414.   "Sets the Nth element of List (zero based) to Newval."
  415.   (if (< n 0)
  416.       (error "~S is an illegal N for SETF of NTH." n)
  417.       (do ((count n (1- count)))
  418.       ((zerop count) (rplaca list newval) newval)
  419.     (declare (fixnum count))
  420.     (if (endp (cdr list))
  421.         (error "~S is too large an index for SETF of NTH." n)
  422.         (setq list (cdr list))))))
  423.  
  424. ;;;; Macros for (&key (key #'identity) (test #'eql testp) (test-not nil notp)).
  425. ;;; Use these with the following keyword args:
  426. ;;;
  427. (defmacro with-set-keys (funcall)
  428.   `(cond ((and testp notp) (error "Test and test-not both supplied."))
  429.      (notp ,(append funcall '(:key key :test-not test-not)))
  430.      (t ,(append funcall '(:key key :test test)))))
  431.  
  432. (defmacro satisfies-the-test (item elt)
  433.   `(cond (testp
  434.       (funcall test ,item (funcall key ,elt)))
  435.      (notp
  436.       (not (funcall test-not ,item (funcall key ,elt))))
  437.      (t (funcall test ,item (funcall key ,elt)))))
  438.  
  439.  
  440. ;;; Substitution of expressions
  441.  
  442.  
  443.  
  444. (defun subst (new old tree &key (key #'identity)
  445.           (test #'eql testp) (test-not nil notp))
  446.   "Substitutes new for subtrees matching old."
  447.   (labels ((s (subtree)
  448.           (cond ((satisfies-the-test old subtree) new)
  449.             ((atom subtree) subtree)
  450.             (t (let ((car (s (car subtree)))
  451.                  (cdr (s (cdr subtree))))
  452.              (if (and (eq car (car subtree))
  453.                   (eq cdr (cdr subtree)))
  454.                  subtree
  455.                  (cons car cdr)))))))
  456.     (s tree)))
  457.  
  458. (defun subst-if (new test tree &key (key #'identity))
  459.   "Substitutes new for subtrees for which test is true."
  460.   (labels ((s (subtree)
  461.           (cond ((funcall test (funcall key subtree)) new)
  462.             ((atom subtree) subtree)
  463.             (t (let ((car (s (car subtree)))
  464.                  (cdr (s (cdr subtree))))
  465.              (if (and (eq car (car subtree))
  466.                   (eq cdr (cdr subtree)))
  467.                  subtree
  468.                  (cons car cdr)))))))
  469.     (s tree)))
  470.  
  471. (defun subst-if-not (new test tree &key (key #'identity))
  472.   "Substitutes new for subtrees for which test is false."
  473.   (labels ((s (subtree)
  474.           (cond ((not (funcall test (funcall key subtree))) new)
  475.             ((atom subtree) subtree)
  476.             (t (let ((car (s (car subtree)))
  477.                  (cdr (s (cdr subtree))))
  478.              (if (and (eq car (car subtree))
  479.                   (eq cdr (cdr subtree)))
  480.                  subtree
  481.                  (cons car cdr)))))))
  482.     (s tree)))
  483.  
  484. (defun nsubst (new old tree &key (key #'identity)
  485.           (test #'eql testp) (test-not nil notp))
  486.   "Substitutes new for subtrees matching old."
  487.   (labels ((s (subtree)
  488.           (cond ((satisfies-the-test old subtree) new)
  489.             ((atom subtree) subtree)
  490.             (t (do* ((last nil subtree)
  491.                  (subtree subtree (Cdr subtree)))
  492.                 ((atom subtree)
  493.                  (if (satisfies-the-test old subtree)
  494.                  (setf (cdr last) new)))
  495.              (if (satisfies-the-test old subtree)
  496.                  (return (setf (cdr last) new))
  497.                  (setf (car subtree) (s (car subtree)))))
  498.                subtree))))
  499.     (s tree)))
  500.  
  501. (defun nsubst-if (new test tree &key (key #'identity))
  502.   "Substitutes new for subtrees of tree for which test is true."
  503.   (labels ((s (subtree)
  504.           (cond ((funcall test (funcall key subtree)) new)
  505.             ((atom subtree) subtree)
  506.             (t (do* ((last nil subtree)
  507.                  (subtree subtree (Cdr subtree)))
  508.                 ((atom subtree)
  509.                  (if (funcall test (funcall key subtree))
  510.                  (setf (cdr last) new)))
  511.              (if (funcall test (funcall key subtree))
  512.                  (return (setf (cdr last) new))
  513.                  (setf (car subtree) (s (car subtree)))))
  514.                subtree))))
  515.     (s tree)))
  516.  
  517. (defun nsubst-if-not (new test tree &key (key #'identity))
  518.   "Substitutes new for subtrees of tree for which test is false."
  519.   (labels ((s (subtree)
  520.           (cond ((not (funcall test (funcall key subtree))) new)
  521.             ((atom subtree) subtree)
  522.             (t (do* ((last nil subtree)
  523.                  (subtree subtree (Cdr subtree)))
  524.                 ((atom subtree)
  525.                  (if (not (funcall test (funcall key subtree)))
  526.                  (setf (cdr last) new)))
  527.              (if (not (funcall test (funcall key subtree)))
  528.                  (return (setf (cdr last) new))
  529.                  (setf (car subtree) (s (car subtree)))))
  530.                subtree))))
  531.     (s tree)))
  532.  
  533.  
  534.  
  535.  
  536. (defun sublis (alist tree &key (key #'identity)
  537.              (test #'eql) (test-not nil notp))
  538.   "Substitutes from alist into tree nondestructively."
  539.   (declare (inline assoc))
  540.   (labels ((s (subtree)
  541.           (let ((assoc
  542.              (if notp
  543.              (assoc (funcall key subtree) alist :test-not test-not)
  544.              (assoc (funcall key subtree) alist :test test))))
  545.         (cond (assoc (cdr assoc))
  546.               ((atom subtree) subtree)
  547.               (t (let ((car (s (car subtree)))
  548.                    (cdr (s (cdr subtree))))
  549.                (if (and (eq car (car subtreE))
  550.                     (eq cdr (cdr subtree)))
  551.                    subtree
  552.                    (cons car cdr))))))))
  553.     (s tree)))
  554.  
  555. ;;; In run-time env, since can be referenced in line expansions.
  556. (defmacro nsublis-macro ()
  557.   '(if notp
  558.        (assoc (funcall key subtree) alist :test-not test-not)
  559.        (assoc (funcall key subtree) alist :test test)))
  560.  
  561. (defun nsublis (alist tree &key (key #'identity)
  562.           (test #'eql) (test-not nil notp))
  563.   "Substitutes new for subtrees matching old."
  564.   (declare (inline assoc))
  565.   (let (temp)
  566.     (labels ((s (subtree)
  567.         (cond ((Setq temp (nsublis-macro))
  568.                (cdr temp))
  569.               ((atom subtree) subtree)
  570.               (t (do* ((last nil subtree)
  571.                    (subtree subtree (Cdr subtree)))
  572.                   ((atom subtree)
  573.                    (if (setq temp (nsublis-macro))
  574.                    (setf (cdr last) (cdr temp))))
  575.                (if (setq temp (nsublis-macro))
  576.                    (return (setf (Cdr last) (Cdr temp)))
  577.                    (setf (car subtree) (s (car subtree)))))
  578.              subtree))))
  579.       (s tree))))
  580.  
  581.  
  582. ;;;; Functions for using lists as sets
  583.  
  584. (defun member (item list &key (key #'identity) (test #'eql testp)
  585.             (test-not nil notp))
  586.   "Returns tail of list beginning with first element satisfying EQLity,
  587.    :test, or :test-not with a given item."
  588.   (do ((list list (cdr list)))
  589.       ((null list) nil)
  590.     (let ((car (car list)))
  591.       (if (satisfies-the-test item car)
  592.       (return list)))))
  593.  
  594. (defun member-if (test list &key (key #'identity))
  595.   "Returns tail of list beginning with first element satisfying test(element)"
  596.   (unless (listp list)
  597.     (error "~S is not a list." list))
  598.   (do ((list list (Cdr list)))
  599.       ((endp list) nil)
  600.     (if (funcall test (funcall key (car list)))
  601.     (return list))))
  602.  
  603. (defun member-if-not (test list &key (key #'identity))
  604.   "Returns tail of list beginning with first element not satisfying test(el)"
  605.   (unless (listp list)
  606.     (error "~S is not a list." list))
  607.   (do ((list list (cdr list)))
  608.       ((endp list) ())
  609.     (if (not (funcall test (funcall key (car list))))
  610.     (return list)))))
  611.  
  612. (defun tailp (sublist list)
  613.   "Returns T if (EQL Sublist (NTHCDR <n> List)) for some value of <n>, NIL
  614.   otherwise."
  615.   (do ((list list (cdr list)))
  616.       ((atom list) (eql list sublist))
  617.     (if (eql sublist list)
  618.     (return t))))
  619.  
  620. (defun adjoin (item list &key (key #'identity) (test #'eql) (test-not nil notp))
  621.   "Add item to list unless it is already a member"
  622.   (declare (inline member))
  623.   (if (if notp (member (funcall key item) list :test-not test-not :key key)
  624.       (member (funcall key item) list :test test :key key))
  625.       list
  626.       (cons item list)))
  627.  
  628.  
  629. ;;; UNION -- Public.
  630. ;;;
  631. ;;; This function assumes list2 is the result, adding to it from list1 as
  632. ;;; necessary.  List2 must initialize the result value, so the call to MEMBER
  633. ;;; will apply the test to the elements from list1 and list2 in the correct
  634. ;;; order.
  635. ;;;
  636. (defun union (list1 list2 &key
  637.             (key #'identity) (test #'eql testp) (test-not nil notp))
  638.   "Returns the union of list1 and list2."
  639.   (declare (inline member))
  640.   (when (and testp notp) (error "Test and test-not both supplied."))
  641.   (let ((res list2))
  642.     (dolist (elt list1)
  643.       (unless (with-set-keys (member (funcall key elt) list2))
  644.     (push elt res)))
  645.     res))
  646.  
  647. ;;; Destination and source are setf-able and many-evaluable.  Sets the source
  648. ;;; to the cdr, and "conses" the 1st elt of source to destination.
  649. ;;;
  650. (defmacro steve-splice (source destination)
  651.   `(let ((temp ,source))
  652.      (setf ,source (cdr ,source)
  653.        (cdr temp) ,destination
  654.        ,destination temp)))
  655.  
  656. (defun nunion (list1 list2 &key (key #'identity)
  657.              (test #'eql testp) (test-not nil notp))
  658.   "Destructively returns the union list1 and list2."
  659.   (declare (inline member))
  660.   (if (and testp notp)
  661.       (error "Test and test-not both supplied."))
  662.   (let ((res list2))
  663.     (do ()
  664.     ((endp list1))
  665.       (if (not (with-set-keys (member (funcall key (car list1)) list2)))
  666.       (steve-splice list1 res)
  667.       (setf list1 (cdr list1))))
  668.     res))
  669.   
  670.  
  671. (defun intersection (list1 list2  &key (key #'identity)
  672.                    (test #'eql testp) (test-not nil notp))
  673.   "Returns the intersection of list1 and list2."
  674.   (declare (inline member))
  675.   (if (and testp notp)
  676.       (error "Test and test-not both supplied."))
  677.   (let ((res nil))
  678.     (dolist (elt list1)
  679.       (if (with-set-keys (member (funcall key elt) list2))
  680.       (push elt res)))
  681.     res))
  682.  
  683. (defun nintersection (list1 list2 &key (key #'identity)
  684.              (test #'eql testp) (test-not nil notp))
  685.   "Destructively returns the intersection of list1 and list2."
  686.   (declare (inline member))
  687.   (if (and testp notp)
  688.       (error "Test and test-not both supplied."))
  689.   (let ((res nil))
  690.     (do () ((endp list1))
  691.       (if (with-set-keys (member (funcall key (car list1)) list2))
  692.       (steve-splice list1 res)
  693.       (setq list1 (Cdr list1))))
  694.     res))
  695.  
  696. (defun set-difference (list1 list2 &key (key #'identity)
  697.                  (test #'eql testp) (test-not nil notp))
  698.   "Returns the elements of list1 which are not in list2."
  699.   (declare (inline member))
  700.   (if (and testp notp)
  701.       (error "Test and test-not both supplied."))
  702.   (if (null list2)
  703.       list1
  704.       (let ((res nil))
  705.     (dolist (elt list1)
  706.       (if (not (with-set-keys (member (funcall key elt) list2)))
  707.           (push elt res)))
  708.     res)))
  709.  
  710.  
  711. (defun nset-difference (list1 list2 &key (key #'identity)
  712.                   (test #'eql testp) (test-not nil notp))
  713.   "Destructively returns the elements of list1 which are not in list2."
  714.   (declare (inline member))
  715.   (if (and testp notp)
  716.       (error "Test and test-not both supplied."))
  717.   (let ((res nil))
  718.     (do () ((endp list1))
  719.       (if (not (with-set-keys (member (funcall key (car list1)) list2)))
  720.       (steve-splice list1 res)
  721.       (setq list1 (cdr list1))))
  722.     res))
  723.  
  724.  
  725. (defun set-exclusive-or (list1 list2 &key (key #'identity)
  726.                    (test #'eql testp) (test-not nil notp))
  727.   "Returns new list of elements appearing exactly once in list1 and list2."
  728.   (declare (inline member))
  729.   (let ((result nil))
  730.     (dolist (elt list1)
  731.       (unless (with-set-keys (member (funcall key elt) list2))
  732.     (setq result (cons elt result))))
  733.     (dolist (elt list2)
  734.       (unless (with-set-keys (member (funcall key elt) list1))
  735.     (setq result (cons elt result))))
  736.     result))
  737.  
  738.  
  739. ;;; The outer loop examines list1 while the inner loop examines list2. If an
  740. ;;; element is found in list2 "equal" to the element in list1, both are
  741. ;;; spliced out. When the end of list1 is reached, what is left of list2 is
  742. ;;; tacked onto what is left of list1.  The splicing operation ensures that
  743. ;;; the correct operation is performed depending on whether splice is at the
  744. ;;; top of the list or not
  745.  
  746. (defun nset-exclusive-or (list1 list2 &key (test #'eql) (test-not nil notp)
  747.                 (key #'identity))
  748.   "Destructively return a list with elements which appear but once in list1
  749.    and list2."
  750.   (do ((x list1 (cdr x))
  751.        (splicex ()))
  752.       ((endp x)
  753.        (if (null splicex)
  754.        (setq list1 list2)
  755.        (rplacd splicex list2))
  756.        list1)
  757.     (do ((y list2 (cdr y))
  758.      (splicey ()))
  759.     ((endp y) (setq splicex x))
  760.       (cond ((if notp
  761.          (not (funcall test-not (funcall key (car x))
  762.                    (funcall key (Car y))))
  763.          (funcall test (funcall key (car x)) (funcall key (Car y))))
  764.          (if (null splicex)
  765.          (setq list1 (cdr x))
  766.          (rplacd splicex (cdr x)))
  767.          (if (null splicey) 
  768.          (setq list2 (cdr y))
  769.          (rplacd splicey (cdr y)))
  770.          (return ()))            ; assume lists are really sets
  771.         (t (setq splicey y)))))))
  772.  
  773. (defun subsetp (list1 list2 &key (key #'identity)
  774.               (test #'eql testp) (test-not nil notp))
  775.   "Returns T if every element in list1 is also in list2."
  776.   (declare (inline member))
  777.   (dolist (elt list1)
  778.     (unless (with-set-keys (member (funcall key elt) list2))
  779.       (return-from subsetp nil)))
  780.   T)
  781.  
  782.  
  783.  
  784. ;;;; :key arg optimization to save funcall of IDENTITY.
  785.  
  786. ;;; We should move this earlier in this file and make other functions use it as
  787. ;;; well.
  788. ;;;
  789.  
  790. ;;; APPLY-KEY saves us a function call sometimes.
  791. ;;;    This is not in and (eval-when (compile eval) ...
  792. ;;;    because this is used in seq.lisp and sort.lisp.
  793. ;;;
  794. (defmacro apply-key (key element)
  795.   `(if ,key
  796.        (funcall ,key ,element)
  797.        ,element))
  798.  
  799. (defun identity (thing)
  800.   "Returns what was passed to it."
  801.   thing)
  802.  
  803.  
  804. (defun complement (function)
  805.   "Builds a new function that returns T whenever FUNCTION returns NIL and
  806.    NIL whenever FUNCTION returns T."
  807.   #'(lambda (&optional (arg0 nil arg0-p) (arg1 nil arg1-p) (arg2 nil arg2-p)
  808.                &rest more-args)
  809.       (not (cond (more-args (apply function arg0 arg1 arg2 more-args))
  810.          (arg2-p (funcall function arg0 arg1 arg2))
  811.          (arg1-p (funcall function arg0 arg1))
  812.          (arg0-p (funcall function arg0))
  813.          (t (funcall function))))))
  814.  
  815.  
  816. (defun constantly (value &optional (val1 nil val1-p) (val2 nil val2-p)
  817.              &rest more-values)
  818.   "Builds a function that always returns VALUE, and posisbly MORE-VALUES."
  819.   (cond (more-values
  820.      (let ((list (list* value val1 val2 more-values)))
  821.        #'(lambda ()
  822.            (declare (optimize (speed 3) (safety 0)))
  823.            (values-list list))))
  824.     (val2-p
  825.      #'(lambda ()
  826.          (declare (optimize (speed 3) (safety 0)))
  827.          (values value val1 val2)))
  828.     (val1-p
  829.      #'(lambda ()
  830.          (declare (optimize (speed 3) (safety 0)))
  831.          (values value val1)))
  832.     (t
  833.      #'(lambda ()
  834.          (declare (optimize (speed 3) (safety 0)))
  835.          value))))
  836.  
  837.  
  838. ;;; Functions that operate on association lists
  839.  
  840. (defun acons (key datum alist)
  841.   "Construct a new alist by adding the pair (key . datum) to alist"
  842.   (cons (cons key datum) alist))
  843.  
  844. (defun pairlis (keys data &optional (alist '()))
  845.   "Construct an association list from keys and data (adding to alist)"
  846.   (do ((x keys (cdr x))
  847.        (y data (cdr y)))
  848.       ((and (endp x) (endp y)) alist)
  849.     (if (or (endp x) (endp y)) 
  850.     (error "The lists of keys and data are of unequal length."))
  851.     (setq alist (acons (car x) (car y) alist))))
  852.  
  853. ;;; In run-time environment, since these guys can be inline expanded.
  854. (defmacro assoc-guts (test-guy)
  855.   `(do ((alist alist (cdr alist)))
  856.        ((endp alist))
  857.      (if (car alist)
  858.      (if ,test-guy (return (car alist))))))
  859.  
  860. (defun assoc (item alist &key key test test-not)
  861.   "Returns the cons in alist whose car is equal (by a given test or EQL) to
  862.    the Item."
  863.   (cond (test (assoc-guts (funcall test item (apply-key key (caar alist)))))
  864.     (test-not (assoc-guts (not (funcall test-not item
  865.                         (apply-key key (caar alist))))))
  866.     (t (assoc-guts (eql item (apply-key key (caar alist)))))))
  867.  
  868. (defun assoc-if (predicate alist &key key)
  869.   "Returns the first cons in alist whose car satisfies the Predicate.  If
  870.    key is supplied, apply it to the car of each cons before testing."
  871.   (assoc-guts (funcall predicate (apply-key key (caar alist)))))
  872.  
  873. (defun assoc-if-not (predicate alist &key key)
  874.   "Returns the first cons in alist whose car does not satisfiy the Predicate.
  875.   If key is supplied, apply it to the car of each cons before testing."
  876.   (assoc-guts (not (funcall predicate (apply-key key (caar alist))))))
  877.  
  878.  
  879. (defun rassoc (item alist &key key test test-not)
  880.   (declare (list alist))
  881.   "Returns the cons in alist whose cdr is equal (by a given test or EQL) to
  882.    the Item."
  883.   (cond (test (assoc-guts (funcall test item (apply-key key (cdar alist)))))
  884.     (test-not (assoc-guts (not (funcall test-not item
  885.                         (apply-key key (cdar alist))))))
  886.     (t (assoc-guts (eql item (apply-key key (cdar alist)))))))
  887.  
  888. (defun rassoc-if (predicate alist &key key)
  889.   "Returns the first cons in alist whose cdr satisfies the Predicate.  If key
  890.   is supplied, apply it to the cdr of each cons before testing."
  891.   (assoc-guts (funcall predicate (apply-key key (cdar alist)))))
  892.  
  893. (defun rassoc-if-not (predicate alist &key key)
  894.   "Returns the first cons in alist whose cdr does not satisfy the Predicate.
  895.   If key is supplied, apply it to the cdr of each cons before testing."
  896.   (assoc-guts (not (funcall predicate (apply-key key (cdar alist))))))
  897.  
  898.  
  899.  
  900. ;;;; Mapping functions.
  901.  
  902. (defun map1 (function original-arglists accumulate take-car)
  903.   "This function is called by mapc, mapcar, mapcan, mapl, maplist, and mapcon.
  904.   It Maps function over the arglists in the appropriate way. It is done when any
  905.   of the arglists runs out.  Until then, it CDRs down the arglists calling the
  906.   function and accumulating results as desired."
  907.  
  908.   (let* ((arglists (copy-list original-arglists))
  909.      (ret-list (list nil)) 
  910.      (temp ret-list))
  911.     (do ((res nil)
  912.      (args '() '()))
  913.     ((dolist (x arglists nil) (if (null x) (return t)))
  914.      (if accumulate
  915.          (cdr ret-list)
  916.          (car original-arglists)))
  917.       (do ((l arglists (cdr l)))
  918.       ((null l))
  919.     (push (if take-car (caar l) (car l)) args)
  920.     (setf (car l) (cdar l)))
  921.       (setq res (apply function (nreverse args)))
  922.       (case accumulate
  923.     (:nconc (setq temp (last (nconc temp res))))
  924.     (:list (rplacd temp (list res))
  925.            (setq temp (cdr temp)))))))
  926.  
  927.  
  928. (defun mapc (function list &rest more-lists)
  929.   "Applies fn to successive elements of lists, returns its second argument."
  930.   (map1 function (cons list more-lists) nil t))
  931.  
  932. (defun mapcar (function list &rest more-lists)
  933.   "Applies fn to successive elements of list, returns list of results."
  934.   (map1 function (cons list more-lists) :list t))
  935.  
  936. (defun mapcan (function list &rest more-lists)
  937.   "Applies fn to successive elements of list, returns NCONC of results."
  938.   (map1 function (cons list more-lists) :nconc t))
  939.  
  940. (defun mapl (function list &rest more-lists)
  941.   "Applies fn to successive CDRs of list, returns ()."
  942.   (map1 function (cons list more-lists) nil nil))
  943.  
  944. (defun maplist (function list &rest more-lists)
  945.   "Applies fn to successive CDRs of list, returns list of results."
  946.   (map1 function (cons list more-lists) :list nil))
  947.  
  948. (defun mapcon (function list &rest more-lists)
  949.   "Applies fn to successive CDRs of lists, returns NCONC of results."
  950.   (map1 function (cons list more-lists) :nconc nil))
  951.  
  952.  
  953. ;;; Functions for compatibility sake:
  954.  
  955. (defun memq (item list)
  956.   "Returns tail of list beginning with first element eq to item"
  957.   (member item list :test #'eq))
  958.  
  959. (defun assq (item alist)
  960.   "Return the first pair of alist where item EQ the key of pair"
  961.   (assoc item alist :test #'eq))
  962.  
  963. (defun delq (item list &optional (n 0 np))
  964.   (declare (fixnum n))
  965.   "Returns list with all (up to n) elements with all elements EQ to ITEM
  966.    deleted"
  967.   (do ((x list (cdr x))
  968.        (splice '()))
  969.       ((or (endp x)
  970.        (and np (zerop n))) list)
  971.     (cond ((eq item (car x))
  972.        (setq n (1- n))
  973.        (if (null splice) 
  974.            (setq list (cdr x))
  975.            (rplacd splice (cdr x))))
  976.       (T (setq splice x)))))    ; move splice along to include element
  977.